package com.witframework.core.log;


import java.util.HashMap;


import org.apache.commons.logging.LogFactory;

public class Log {

	public static final int DEBUG = 10000;
	public static final int INFO = 20000;
	public static final int WARN = 30000;
	public static final int ERROR = 40000;
	public static final int FATAL = 50000;
	private static HashMap loggers = new HashMap();

	private Log() {
	}

	public static void write(Object message, int level, String methodName,
			Class classObject) {
		String className = classObject.getName();
		org.apache.commons.logging.Log logger = null;
		String messageStr = methodName + " " + message;
		if (!loggers.containsKey(className)) {
			logger = LogFactory.getLog(className);
			loggers.put(className, logger);
		} else {
			logger = (org.apache.commons.logging.Log) loggers.get(className);
		}
		switch (level) {
		case 10000:
			logger.debug(messageStr);
			break;

		case 20000:
			logger.info(messageStr);
			break;

		case 30000:
			logger.warn(messageStr);
			break;

		case 40000:
			logger.error(messageStr);
			break;

		case 50000:
			logger.fatal(messageStr);
			break;
		}
	}

	public static boolean isDebugEnabled(Class classObject) {
		String className = classObject.getName();
		org.apache.commons.logging.Log logger = null;
		if (!loggers.containsKey(className)) {
			logger = LogFactory.getLog(className);
			loggers.put(className, logger);
		} else {
			logger = (org.apache.commons.logging.Log) loggers.get(className);
		}
		return logger.isDebugEnabled();
	}

	public static void write(Object message, Throwable throwable, int level,
			String methodName, Class classObject) {
		String className = classObject.getName();
		org.apache.commons.logging.Log logger = null;
		String messageStr = methodName + " " + message;
		if (!loggers.containsKey(className)) {
			logger = LogFactory.getLog(className);
			loggers.put(className, logger);
		} else {
			logger = (org.apache.commons.logging.Log) loggers.get(className);
		}
		switch (level) {
		case 10000:
			logger.debug(messageStr, throwable);
			break;

		case 20000:
			logger.info(messageStr, throwable);
			break;

		case 30000:
			logger.warn(messageStr, throwable);
			break;

		case 40000:
			logger.error(messageStr, throwable);
			break;

		case 50000:
			logger.fatal(messageStr, throwable);
			break;
		}
	}

}

